#include <stdint.h>
#include <stdio.h>

/*
例子: 0x12, 0x0F --> "120F"
*/
void Hex2String(uint8_t *des,uint8_t *src, uint16_t src_len)
{
	uint8_t hex_char[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    uint8_t temp;
    int i;

	if(des == NULL || src == NULL || src_len == 0)
		return;
	
    for (i = 0; i < src_len; i++)
    {
        temp = (src[i] & 0xf0) >> 4;
        des[2*i] = hex_char[temp];
        temp = src[i] & 0x0f;
        des[2*i+1] = hex_char[temp];
    }

    des[src_len*2] = '\0';
}

/*
例子: "AABBFD1122" --> 0xAA, 0xBB, 0xFD, 0x11 0x22
*/
uint16_t String2HexArray(char *str, uint8_t *hex)
{
	uint8_t temp = 0;
	uint16_t len = 0;
	
	if(str == NULL || hex == NULL)
		return;
	
	while(*str)
	{
		if((*str >= '0') && (*str <= '9'))
			temp = *str - 0x30;
		else if((*str >= 'A') && (*str <= 'F'))
			temp = *str - 0x37;
		else if((*str >= 'a') && (*str <= 'f'))
			temp = *str - 0x57;
		
		len++;
		if(len % 2 == 1)
		{
			*hex = temp << 4;
		}
		else
		{
			*hex += temp;
			hex++;
		}
		
		str++;
	}
	
	return len/2;
}


